import java.awt.Color;
import java.awt.Font;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Random;

import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.plugin.PlugIn;
import ij.process.ImageProcessor;

// on fabrique le masque et on sauve les valeurs utilisees
// pour les programmes qui ont besoin de ce masque

public class CreateBlackMask extends ImagePlus implements PlugIn{
	
	private ImageProcessor primeIP; 
	private int X=2800,Y=2100; // image size
	
	private ImagePlus inverted;  
	private ImageProcessor inv_ip;  

	
	private boolean[][] masks={{true,true,false,false},{false,false,true,true},{true,false,false,true},
			                   {false,true,true,false},{true,false,true,false},{false,true,false,true}};
	
	
	private Random generator=new Random(1214); 
	
	private int masque[]={0xFF0000,0x00FF00,0x0000FF}; 
	private int decal[]={16,8,0};
	private int col=0; 
	
	public void run(String arg) {
		System.out.println("go");
		
		ImagePlus imp; 


		imp=IJ.openImage("C:/Users/decomite/Pictures/povray/VCS/cmyk/MonaCyan1Dithered.png");
		
		imp.setTitle("PrimeIP");
		
	
		System.out.println(imp); 
		primeIP=imp.getProcessor();
		X=primeIP.getWidth(); 
		Y=primeIP.getHeight(); 
		
		
        setStack(null, imp.getStack());
        setCalibration(imp.getCalibration()); 
		
        	show();
		
		
		

		

		
		primeIP.setFont(new Font(null,Font.ITALIC,60));
		primeIP.setColor(Color.RED);
		primeIP.setLineWidth(10); 
		
	
		setProcessor("Verifying", primeIP);
		
	
	
 			inverted = NewImage.createRGBImage ("blackmask",2*X,2*Y, 1, NewImage.FILL_WHITE);
 			inv_ip = inverted.getProcessor();
 			int memory[][]=new int[X][Y]; 
 			int compteur=0; 
 			for(int i=0;i<2*X;i+=2)
 				for(int j=0;j<2*Y;j+=2){
 					int dice=generator.nextInt(6); 
 				 if(compteur++<100) System.out.print(dice+" ");
 				 if(compteur==100) System.out.println(); 
 					memory[i/2][j/2]=dice; 
 					inv_ip.putPixel(i, j,  masks[dice][0] ? 0x000000:0xFFFFFF); 
 					inv_ip.putPixel(i, j+1,  masks[dice][1] ? 0x000000:0xFFFFFF); 
 					inv_ip.putPixel(i+1, j,  masks[dice][2] ? 0x000000:0xFFFFFF); 
 					inv_ip.putPixel(i+1, j+1,  masks[dice][3] ? 0x000000:0xFFFFFF); 
 					
 				}
 			DataOutputStream output; 
 			try{
 				compteur=0; 
 				output=new DataOutputStream(new FileOutputStream("C:/Users/decomite/Pictures/povray/VCS/cmyk/BlackMaskValues.txt"));
 				for(int i=0;i<X;i++)
 					for(int j=0;j<Y;j++) {output.write(memory[i][j]);
 					if(compteur++<100) System.out.print("** "+ memory[i][j]); 
 					}
 				System.out.println("\n Compteur : "+compteur); 
 				output.close(); 
 			}
 			catch(Exception e){System.out.println("Probleme : "+e); System.exit(0);}
		
 					
 			
 		
     
 			System.out.println("toto");
 			
 			inverted.show();
 			
 			System.out.println("end !");
	}
	
	
	

}
